﻿using AutoMapper;
using Customer.API.Applcation.Command.FinanceCommand;
using Customer.Domain;
using Customer.Infrstructre.FinanceManager;
using MediatR;

namespace Customer.API.Applcation.CommandHandlers.FinanceCommandHandler
{
    /// <summary>
    /// 新增财务应收中介者
    /// </summary>
    public class ADDReceivableCommandHandler:IRequestHandler<ADDReceivableCommand,int>
    {
        private readonly IReceivableRepository _receRep;
        private readonly IMapper mapper;

        public ADDReceivableCommandHandler(IReceivableRepository receRep, IMapper mapper)
        {
            _receRep = receRep;
            this.mapper = mapper;
        }

        public async Task<int> Handle(ADDReceivableCommand request, CancellationToken cancellationToken)
        {
            var res = (await _receRep.GetAllAsync()).Where(x => x.ReceivableName == request.dto.ReceivableName);
            if (res.Count() > 0)
            {
                return -1;//应收管理名称已存在
            }
            return await _receRep.AddAsync(mapper.Map<Receivable>(request.dto));
        }
    }
}
